package com.errol.batchprocessor.payroll.advisorimpl.common;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.renjia.constant.Constant;
import com.renjia.dao.BusPayrollRecordDetailMapper;
import com.renjia.entity.BusPayrollRecord;
import com.renjia.entity.BusPayrollRecordDetail;
import com.renjia.enums.*;
import com.renjia.service.SendWechatMessageService;
import com.renjia.service.generic.support.customer.CustomerChannelSupportService;
import com.renjia.service.generic.support.customer.bo.ProtocolAndSignatureBO;
import com.renjia.service.module.employee.EmployeeManageService;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeeBatchPayload;
import com.renjia.service.module.payroll.advisor.upload.PayrollUploadCheckAdvisor;
import com.renjia.service.module.payroll.advisor.upload.PayrollUploadCreateAdvisor;
import com.renjia.service.module.payroll.advisor.upload.PayrollUploadPostAdvisor;
import com.renjia.service.module.payroll.advisorimpl.payload.PayrollUploadBatchPayload;
import com.renjia.service.module.payroll.advisorimpl.payload.PayrollUploadSinglePayload;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

/**
 * 费用发放默认执行扩展
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2021/03/17
 * @since v5.0.6
 **/
@Service
public class PayDefaultAdvisorImpl implements PayrollUploadCheckAdvisor, PayrollUploadCreateAdvisor, PayrollUploadPostAdvisor {

    private final SendWechatMessageService      sendWechatMessageService;
    private final CustomerChannelSupportService customerChannelSupportService;
    private final EmployeeManageService         employeeManageService;
    private final BusPayrollRecordDetailMapper  busPayrollRecordDetailMapper;

    public PayDefaultAdvisorImpl(SendWechatMessageService sendWechatMessageService, CustomerChannelSupportService customerChannelSupportService,
                                 EmployeeManageService employeeManageService, BusPayrollRecordDetailMapper busPayrollRecordDetailMapper) {
        this.sendWechatMessageService = sendWechatMessageService;
        this.customerChannelSupportService = customerChannelSupportService;
        this.employeeManageService = employeeManageService;
        this.busPayrollRecordDetailMapper = busPayrollRecordDetailMapper;
    }

    @Override
    public boolean shallProcess(@NonNull PayrollEmployeeBatchPayload advisorPayload) {
        return true;
    }

    @Override
    public int priority() {
        return 51;
    }

    @Override
    public boolean doCheckBeforePayrollSavedAtUploadStage(@NonNull PayrollUploadBatchPayload batchPayload) {
        if (batchPayload.getPayrollDetailExcelPoList().size() > Constant.COMMON_MAX_SIZE) {
            batchPayload.getErrMsgList().add(ExceptionEnum.PUT_OUT_SIZE.getMsg());
            return false;
        }
        if (StringUtils.isNoneBlank(batchPayload.getCoreCustomerChannelMerchant().getUpperChannelMerchantId())) {
            batchPayload.getErrMsgList().add("上级渠道商代发功能开发中");
            return false;
        }
        // 上传批次时检查签约所需配置
        ProtocolAndSignatureBO protocolAndSignature = customerChannelSupportService.getProtocolAndSignatureBy(batchPayload.getCoreCustomerChannelMerchant().getCustomerId(),
            batchPayload.getCoreCustomerChannelMerchant().getChannelMerchantId());
        if (protocolAndSignature == null || !protocolAndSignature.isSuccess()) {
            batchPayload.getImportBatch().setBatchStatusIntro("客户未配置协议或服务商未配置签章");
            batchPayload.getErrMsgList().add("客户未配置协议或服务商未配置签章");
            return false;
        }
        batchPayload.setProtocolAndSignatureBO(protocolAndSignature);
        return true;
    }

    @Override
    public boolean doAfterNewPayrollRecordAtUploadStage(@NonNull PayrollUploadBatchPayload batchPayload) {
        // 基础参数赋值
        if (StringUtils.isEmpty(batchPayload.getBatchEntity().getId())) {
            batchPayload.getBatchEntity().setId(IdWorker.get32UUID());
        }
        batchPayload.getBatchEntity().setBatchNo(batchPayload.getBatchNo());
        batchPayload.getBatchEntity().setPayrollAdvisorType(batchPayload.getPayrollAdvisorTypeEnum().getCode());
        batchPayload.getBatchEntity().setCreateAndUpdateUser(batchPayload.getCoreUser());
        batchPayload.getBatchEntity().setPaySelectType(PaySelectTypeEnum.BATCH.getCode());
        // 提交参数赋值
        batchPayload.getBatchEntity().setAnnotation(batchPayload.getPayrollRecordPo().getAnnotation());
        batchPayload.getBatchEntity().setShowPaySlip(batchPayload.getPayrollRecordPo().getShowPaySlip());
        batchPayload.getBatchEntity().setGrantExplain(batchPayload.getPayrollRecordPo().getGrantExplain());
        //v5.1.5添加
        batchPayload.getBatchEntity().setRelatedCustomerCode(batchPayload.getPayrollRecordPo().getCustomerCode());
        batchPayload.getBatchEntity().setAssociationCustomerId(batchPayload.getPayrollRecordPo().getAssociationCustomerId());
        batchPayload.getBatchEntity().setAssociationCustomerName(batchPayload.getPayrollRecordPo().getAssociationCustomerName());
        // 客户-服务商 冗余
        batchPayload.getBatchEntity().setChannelMerchantId(batchPayload.getCoreCustomerChannelMerchant().getChannelMerchantId());
        batchPayload.getBatchEntity().setChannelMerchantName(batchPayload.getCoreCustomerChannelMerchant().getChannelMerchantName());
        batchPayload.getBatchEntity().setCustomerId(batchPayload.getCoreCustomerChannelMerchant().getCustomerId());
        batchPayload.getBatchEntity().setCustomerName(batchPayload.getCoreCustomerChannelMerchant().getCustomerName());
        batchPayload.getBatchEntity().setRemark0(batchPayload.getCoreCustomerChannelMerchant().getRemark0());
        // 是否自动发放
        batchPayload.getBatchEntity().setPayOn(batchPayload.getCoreCustomerChannelMerchant().getPayOn());
        return true;
    }

    @Override
    public boolean doAfterNewPayrollDetailAtUploadStage(@NonNull PayrollUploadSinglePayload singlePayload, @NonNull PayrollUploadBatchPayload batchPayload) {
        // 基础参数赋值
        if (StringUtils.isEmpty(singlePayload.getBusPayrollRecordDetail().getId())) {
            singlePayload.getBusPayrollRecordDetail().setId(IdWorker.get32UUID());
        }
        singlePayload.getBusPayrollRecordDetail().setBusPayrollRecordId(batchPayload.getBatchEntity().getId());
        singlePayload.getBusPayrollRecordDetail().setBatchNo(batchPayload.getBatchNo());
        singlePayload.getBusPayrollRecordDetail().setCreateAndUpdateUser(batchPayload.getCoreUser());
        // 提交参数赋值
        singlePayload.getBusPayrollRecordDetail().setAnnotation(batchPayload.getPayrollRecordPo().getAnnotation());
        singlePayload.getBusPayrollRecordDetail().setShowPaySlip(batchPayload.getPayrollRecordPo().getShowPaySlip());
        singlePayload.getBusPayrollRecordDetail().setGrantExplain(batchPayload.getPayrollRecordPo().getGrantExplain());
        //v5.1.5添加
        singlePayload.getBusPayrollRecordDetail().setAssociationCustomerId(batchPayload.getPayrollRecordPo().getAssociationCustomerId());
        singlePayload.getBusPayrollRecordDetail().setAssociationCustomerName(batchPayload.getPayrollRecordPo().getAssociationCustomerName());
        singlePayload.getBusPayrollRecordDetail().setRelatedCustomerCode(batchPayload.getPayrollRecordPo().getCustomerCode());
        // 人员发放信息
        singlePayload.getBusPayrollRecordDetail().setDocumentType(DocumentTypeEnum.ID.getCode());
        singlePayload.getBusPayrollRecordDetail().setDocumentNumber(singlePayload.getPayrollDetailPo().getDocumentNumber());
        singlePayload.getBusPayrollRecordDetail().setEmployeeName(singlePayload.getPayrollDetailPo().getEmployeeName());
        singlePayload.getBusPayrollRecordDetail().setMobile(singlePayload.getPayrollDetailPo().getMobile());
        singlePayload.getBusPayrollRecordDetail().setRemark0(singlePayload.getPayrollDetailPo().getBankCard());
        singlePayload.getBusPayrollRecordDetail().setMoneyAttribute(singlePayload.getPayrollDetailPo().getMoneyAttribute());
        singlePayload.getBusPayrollRecordDetail().setDemandAchievement(singlePayload.getPayrollDetailPo().getDemandAchievement());
        // 5.2.6 允许发放上传收款支行信息
        singlePayload.getBusPayrollRecordDetail().setRemark2(singlePayload.getPayrollDetailPo().getInBankCode());
        singlePayload.getBusPayrollRecordDetail().setRemark3(singlePayload.getPayrollDetailPo().getInBankName());
        // 客户-服务商 冗余
        singlePayload.getBusPayrollRecordDetail().setChannelMerchantId(batchPayload.getCoreCustomerChannelMerchant().getChannelMerchantId());
        singlePayload.getBusPayrollRecordDetail().setChannelMerchantName(batchPayload.getCoreCustomerChannelMerchant().getChannelMerchantName());
        singlePayload.getBusPayrollRecordDetail().setCustomerId(batchPayload.getCoreCustomerChannelMerchant().getCustomerId());
        singlePayload.getBusPayrollRecordDetail().setCustomerName(batchPayload.getCoreCustomerChannelMerchant().getCustomerName());
        // 是否自动发放，当前流程应该没有自动发放
        singlePayload.getBusPayrollRecordDetail().setPayOn(batchPayload.getCoreCustomerChannelMerchant().getPayOn());
        return true;
    }

    @Override
    public boolean doBeforeModifyPayrollDetailAtUploadStage(@NonNull PayrollUploadSinglePayload singlePayload, @NonNull PayrollUploadBatchPayload batchPayload) {
        //v5.3.0单个编辑配置可以人员重复不校验
        if (SwitchEnum.ON.getCode2().equals(batchPayload.getCoreCustomerChannelMerchant().getAllowEmployeeRepeat())) {
            // 新增，或者编辑了身份证号，检查同一批次身份证是否重复
            if (busPayrollRecordDetailMapper.selectCount(
                Condition.<BusPayrollRecordDetail> wrapper().eq("batch_no", batchPayload.getBatchNo()).eq("document_number", singlePayload.getPayrollDetailPo().getDocumentNumber())
                        .eq("valid_status",PayrollDetailValidateStatusEnum.VALIDATE_PASS.getCode())
                    .ne(StringUtils.isNotBlank(singlePayload.getBusPayrollRecordDetail().getId()), "id", singlePayload.getBusPayrollRecordDetail().getId())) > 0) {
                singlePayload.getErrMsgList().add("批次内身份证号重复");
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean doAfterPayrollSavedAtUploadStage(@NonNull PayrollUploadBatchPayload batchPayload) {
        employeeManageService.importAsyncByPayrollRecord(batchPayload);

        /*仅线上发放*/
        if (PayTypeEnum.ONLINE.getCode().toString().equals(batchPayload.getCoreCustomerChannelMerchant().getRemark0())) {
            //公众号消息推送
            BusPayrollRecord busPayrollRecord = batchPayload.getBatchEntity();
            sendWechatMessageService.sendForPayroll(busPayrollRecord.getCreateUserId(), busPayrollRecord.getCustomerId(), busPayrollRecord.getChannelMerchantId(), "发放提交", "",
                busPayrollRecord.getBatchNo(), busPayrollRecord.getCustomerName(), busPayrollRecord.getChannelMerchantName(), busPayrollRecord.getShouldPayAmount().toString());
        }
        return true;
    }
}
